<?php
class Touroperators_model extends CRM_Model{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function GetDS(){
		$this->db->select('_touroperators.rid as rid,
							_touroperators.stouroperator_name as stouroperator_name,  
							_touroperators.touroperator_name as touroperator_name,  
							_touroperators.license as license, 
							_touroperators.chief_name as chief_name, 
							_touroperators.contact_phone as contact_phone, 
							_touroperators.contact_email as contact_email, 
							_touroperators.www as www,
							_touroperators.contract as contract, 
							DATE_FORMAT(_touroperators.contract_period, \'%d.%m.%Y\') as contract_period,
							_touroperators.adress as adress,
							_touroperators.fax as fax,
							_touroperators.contact_person as contact_person,
							DATE_FORMAT(_touroperators.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT, 
							_touroperators.descr as descr, _touroperators.archive');
		$this->db->from('_touroperators');
		if($sort = $this->ciObject->GetSessionData('sort')){
			$this->db->orderby($sort['c'], $sort['r']);
		}
		$query = $this->DBGet('_touroperators');
		if($query->num_rows()>0) return $this->ciObject->GetPagingDS($query->result());
		return array();
	}
	
	public function GetEditDS(){
		$this->db->select('_touroperators.rid as rid, 
							_touroperators.stouroperator_name as stouroperator_name,
							_touroperators.touroperator_name as touroperator_name,  
							_touroperators.license as license, 
							_touroperators.chief_name as chief_name, 
							_touroperators.contact_phone as contact_phone, 
							_touroperators.contact_email as contact_email, 
							_touroperators.www as www,
							_touroperators.contract as contract, 
							DATE_FORMAT(_touroperators.contract_period, \'%d.%m.%Y\') as contract_period,
							_touroperators.adress as adress,
							_touroperators.fax as fax,
							_touroperators.contact_person as contact_person,
							DATE_FORMAT(_touroperators.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT, 
							_touroperators.descr as descr, _touroperators.archive');
		$this->db->from('_touroperators');
		$this->db->where(array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->DBGet('_touroperators');
		if($query->num_rows()>0) return $query->row();
		return False;
	}
	
	public function CreateRecord(){
		$insArray = array('touroperator_name'=>$_POST['touroperator_name'],  
							'stouroperator_name'=>$_POST['stouroperator_name'], 
							'license'=>$_POST['license'], 
							'chief_name'=>$_POST['chief_name'], 
							'contact_phone'=>$_POST['contact_phone'], 
							'contact_email'=>$_POST['contact_email'], 
							'www'=>$_POST['www'],
							'contract'=>$_POST['contract'], 
							'contract_period'=>$this->toMySQLDate($_POST['contract_period']), 
							'adress'=>$_POST['adress'],
							'fax'=>$_POST['fax'],
							'contact_person'=>$_POST['contact_person'],
							'descr'=>$_POST['descr'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->trans_begin();
		$this->db->insert('_touroperators', $insArray);
		$insRid = $this->db->insert_id();
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $insRid;
		}		
	}
	
	public function UpdateRecord(){
		$updateArray = array('touroperator_name'=>$_POST['touroperator_name'],  
							'stouroperator_name'=>$_POST['stouroperator_name'], 
							'license'=>$_POST['license'], 
							'chief_name'=>$_POST['chief_name'], 
							'contact_phone'=>$_POST['contact_phone'], 
							'contact_email'=>$_POST['contact_email'], 
							'www'=>$_POST['www'],
							'contract'=>$_POST['contract'], 
							'contract_period'=>$this->toMySQLDate($_POST['contract_period']),
							'adress'=>$_POST['adress'],
							'fax'=>$_POST['fax'],
							'contact_person'=>$_POST['contact_person'],
							'descr'=>$_POST['descr'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->trans_begin();
		$this->db->update('_touroperators', $updateArray, array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
	
	public function DeleteRecord(){
		$this->db->trans_begin();
		$this->db->delete('_touroperators', array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
	
	public function GetPair($p_value = 'rid', $p_name='stouroperator_name'){
		$this->db->select("_touroperators.{$p_value}, _touroperators.{$p_name}");
		$this->db->from('_touroperators');
		$this->db->orderby("_touroperators.{$p_name}");
		$query = $this->db->get();
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetPairArr($p_value = 'rid', $p_name='stouroperator_name'){
		$this->db->select("_touroperators.{$p_value}, _touroperators.{$p_name}");
		$this->db->from('_touroperators');
		$this->db->orderby("_touroperators.{$p_name}");
		$query = $this->db->get();
		if($query->num_rows()>0) {
			$result = array(' '=>'');
			foreach($query->result() as $item) $result[$item->$p_value]=$item->$p_name;
			return $result;
		}
		return False;
	}
	
}
?>